/**
 * 自定义
 * */

//var apiDomain = 'http://www.jiuge_tiepi.com/api/admin';
var adminLogin = 'adminInfo';
var apis = {
    login: { //登录
        method: 'post',
        url: apiDomain + '/login',
    },
    leftMenu: { //登录
        method: 'post',
        url: apiDomain + '/leftmenu',
    },
    actionIndex: { //功能列表
        method: 'post',
        url: apiDomain + '/action/index',
    },
    actionParent: {
        method: 'post',
        url: apiDomain + '/action/parent',
    },
    actionOne: {
        method: 'post',
        url: apiDomain + '/action/one',
    },
    actionUpdate: {
        method: 'post',
        url: apiDomain + '/action/update',
    },
    actionDel: {
        method: 'post',
        url: apiDomain + '/action/del',
    },
    adminIndex: { //管理员列表
        method: 'post',
        url: apiDomain + '/admin/index',
    },
    adminOne: { //管理员一个
        method: 'post',
        url: apiDomain + '/admin/one',
    },
    adminSave: { //管理员保存
        method: 'post',
        url: apiDomain + '/admin/save',
    },
    adminPwd: { //管理员密码
        method: 'post',
        url: apiDomain + '/admin/pwd',
    },
    caseCateOption: { //案例分类选项
        method: 'post',
        url: apiDomain + '/case/cate/option',
    },
    caseCateIndex: { //案例分类列表
        method: 'post',
        url: apiDomain + '/case/cate/index',
    },
    caseCateModify: { //案例分类修改
        method: 'post',
        url: apiDomain + '/case/cate/modify',
    },
    caseCateDel: { //案例分类删除
        method: 'post',
        url: apiDomain + '/case/cate/del',
    },
    caseIndex: { //案例列表
        method: 'post',
        url: apiDomain + '/case/index',
    },
    caseOne: { //案例详情
        method: 'post',
        url: apiDomain + '/case/one',
    },
    caseModify: { //案例修改
        method: 'post',
        url: apiDomain + '/case/modify',
    },
    caseDel: { //案例删除
        method: 'post',
        url: apiDomain + '/case/del',
    },
    casexqIndex: { //案例列表
        method: 'post',
        url: apiDomain + '/casexq/index',
    },
    casexqOne: { //案例详情
        method: 'post',
        url: apiDomain + '/casexq/one',
    },
    casexqModify: { //案例修改
        method: 'post',
        url: apiDomain + '/casexq/modify',
    },
    casexqDel: { //案例删除
        method: 'post',
        url: apiDomain + '/casexq/del',
    },
    devicePasIndex: { //设备参数列表
        method: 'post',
        url: apiDomain + '/device/pas/index',
    },
    devicePasModify: { //设备参数更新
        method: 'post',
        url: apiDomain + '/device/pas/modify',
    },
    devicePasDel: { //设备参数删除
        method: 'post',
        url: apiDomain + '/device/pas/del',
    },
    deviceOption: { //设备选项
        method: 'post',
        url: apiDomain + '/device/option',
    },
    deviceIndex: { //设备列表
        method: 'post',
        url: apiDomain + '/device/index',
    },
    imageUpload: { //图片上传
        method: 'post',
        url: apiDomain + '/image/upload',
    },
    userIndex: { //用户列表
        method: 'post',
        url: apiDomain + '/user/index',
    },
    caseComIndex: { //公司列表
        method: 'post',
        url: apiDomain + '/case/company/index',
    },
    caseComModify: { //公司修改
        method: 'post',
        url: apiDomain + '/case/company/modify',
    },
    caseComDel: { //公司删除
        method: 'post',
        url: apiDomain + '/case/company/del',
    },
    caseDesignerOption: { //设计师选项
        method: 'post',
        url: apiDomain + '/case/designer/option',
    },
    caseDesignerIndex: { //设计师列表
        method: 'post',
        url: apiDomain + '/case/designer/index',
    },
    caseDesignerOne: { //设计师详情
        method: 'post',
        url: apiDomain + '/case/designer/one',
    },
    caseDesignerModify: { //设计师修改
        method: 'post',
        url: apiDomain + '/case/designer/modify',
    },
    caseDesignerDel: { //设计师删除
        method: 'post',
        url: apiDomain + '/case/designer/del',
    },
    cxOrderIndex: { //案例需求订单列表
        method: 'post',
        url: apiDomain + '/casexq/order/index',
    },
    cxOrderSure: { //案例需求订单确认
        method: 'post',
        url: apiDomain + '/casexq/order/sure',
    },
    cxOrderFinish: { //案例需求订单完成
        method: 'post',
        url: apiDomain + '/casexq/order/finish',
    },
    cxOrderOne: { //案例需求订单-详情
        method: 'post',
        url: apiDomain + '/casexq/order/one',
    },
    cxOrderUploadImg: { //案例需求订单-上传图片
        method: 'post',
        url: apiDomain + '/casexq/order/upload/img',
    },
    cxOrderUploadVideo: { //案例需求订单-上传视频
        method: 'post',
        url: apiDomain + '/casexq/order/upload/video',
    },
    cxOrderUploadZip: { //案例需求订单-上传ZIP
        method: 'post',
        url: apiDomain + '/casexq/order/upload/zip',
    },
};


(function () {
    if (window.location.pathname != '/admin/login.html') {
        header();
        leftMenu();
    }
})();


//顶部header
function header(){
    var adminInfo = getAdminInfo();
    var h = '';
    h += '<style type="text/css"> #apiDoc { color: #fff; cursor: pointer; position: absolute; top: 10px; left: 200px; } #apiDoc:hover { color: #0088cc; } </style>';
    h += '<div class="navbar navbar-fixed-top">';
    h += '<div class="container-fluid cl">';
    h += '<a class="logo navbar-logo f-l mr-10 hidden-xs" href="index.html">';
    h += '得然版权后台管理';
    h += '</a>';
    //h += '<a href="https://www.eolink.com/share/index?shareCode=a909486f822c1c094b178cf67a7b360c" target="_blank" id="apiDoc">接口文档</a>';
    h += '<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">';
    h += '<ul class="cl">';
    h += '<li>系统管理员</li>';
    h += '<li class="dropDown dropDown_hover">';
    h += '<a href="javascript:;" class="dropDown_A">';
    h += '<span>' + adminInfo.adminName + '</span> ';
    h += '<i class="Hui-iconfont">&#xe6d5;</i>';
    h += '</a>';
    h += '<ul class="dropDown-menu menu radius box-shadow">';
    h += '<li><a href="javascript:;" onclick="logout();">退出</a></li>';
    h += '</ul>';
    h += '</li>';
    h += '</ul>';
    h += '</nav>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('.navbar-wrapper').html(h);
}


////左侧菜单
//function leftMenu(){
//    var adminInfo = getAdminInfo()
//    if (!adminInfo) {
//        layerMsgErr('没有登录');
//        setTimeout(function(){
//            window.location.href = 'login.html';
//        }, 1000);
//        return
//    }
//    $.ajax({
//        type: apis.leftMenu.method,
//        url: apis.leftMenu.url,
//        data: { aid: adminInfo.adminid },
//        success: function (res) {
//            //console.log(res);
//            if (res.code == -3) {
//                msgPop({msg: '登录已过期'});
//                setTimeout(function(){
//                    window.location.href = 'login.html';
//                }, 1000);
//                return
//            }
//            if (res.code < 0) {
//                console.log(res); return
//            }
//            leftMenuHtml(res.data);
//        },
//        error: function () {
//            console.log('错误了！');
//        }
//    });
//}
//左侧菜单
function leftMenu(){
    var ms = [
        { id: 1, title: '首页统计', icon: '&#xe61a;', link: '/admin/index.html', childs: [] },
        { id: 2, title: '系统管理', icon: '&#xe62d;', link: '', childs: [
            { id: 201, title: '管理员', link: '/admin/admin.html' },
        ] },
        { id: 3, title: '原创商城', icon: '&#xe60d;', link: '', childs: [
            { id: 301, title: '案例分类', link: '/admin/case-cate.html' },
            { id: 302, title: '成品案例(案例导览)', link: '/admin/case.html' },
            { id: 303, title: '需求发布(案例上传)', link: '/admin/casexq.html' },
            { id: 304, title: '订单管理', link: '/admin/views/case/order.html' },
        ] },
        { id: 4, title: '用户管理', icon: '&#xe60d;', link: '', childs: [
            { id: 401, title: '设计公司', link: '/admin/views/case/company.html' },
            { id: 402, title: '设计师', link: '/admin/views/case/designer.html' },
            { id: 403, title: '普通用户', link: '/admin/user.html' },
        ] },
    ];
    leftMenuHtml(ms);
}
//菜单字符串
function leftMenuHtml(ms){
    var preUrl = window.location.pathname; //当前相对路径
    var h = '', i;
    h += '<style type="text/css">.mlyjt { ;margin-right: 10px; float: right; } .mla { background: #ffffff; }</style>';
    for (i = 0; i < ms.length; i ++) {
        h += '<dl>';
        if (ms[i].childs.length > 0) {
            h += '<dt onclick="lmQiehuan(' + ms[i].id + ');">';
            //h += '<i class="Hui-iconfont">' + ms[i].icon + '</i>';
            h += ' &nbsp;' + ms[i].title;
            //h += '<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>';
            h += '</dt>';
            h += '</dt>';
        } else {
            if (preUrl == ms[i].link) {
                h += '<a href="' + ms[i].link + '" class="mla">';
            } else {
                h += '<a href="' + ms[i].link + '">';
            }
            h += '<dt>';
            //h += '<i class="Hui-iconfont">' + ms[i].icon + '</i>';
            h += ' &nbsp;' + ms[i].title;
            if (preUrl == ms[i].link) {
                h += '<span class="mlyjt">→</span>';
            }
            h += '</dt>';
            h += '</a>';
        }
        h += '<dd style="display: block;" id="lmChild' + ms[i].id + '">';
        if (ms[i].childs.length > 0) {
            h += '<ul>';
            var childs = ms[i].childs;
            for (var ii = 0; ii < childs.length; ii ++) {
                h += '<li>';
                if (preUrl == childs[ii].link) {
                    h += '<a href="' + childs[ii].link + '" class="mla">';
                } else {
                    h += '<a href="' + childs[ii].link + '">';
                }
                h += childs[ii].title;
                if (preUrl == childs[ii].link) {
                    h += '<span class="mlyjt">→</span>';
                }
                h += '</a>';
                h += '</li>';
            }
            h += '</ul>';
        }
        h += '</dd>';
        h += '</dt>';
        h += '';
        h += '</dl>';
    }
    //console.log(h);
    $('#leftMenu').html(h);
}
//切换菜单
function lmQiehuan(v){
    $('#lmChild' + v).toggle(500);
}


//分页字符串
function pageHtml(e){
    console.log(e);
    var start = e.start, limit = e.limit, total = e.total, url = e.url;
    var pageTotal = Math.ceil(total / limit);
    var h = '';
    h += '<style type="text/css"> #pageHtml {margin: 10px 0; width: 100%; text-align: center; color: #555; } #pageBtn {  color:#555; } #pageBtn a { padding: 5px 10px; color: #0088cc; } </style>';
    h += '显示 <span id="pageCurr">' + start + '</span> / <span id="pageTotal">' + pageTotal + '</span> 页，';
    h += '<span id="pageBtn">';
    h += '<a href="javascript:;" onclick="pageHome();">首页</a>';
    h += '<a href="javascript:;" onclick="pagePre();">上一页</a>';
    //h += '<a href="javascript:;" onclick="msgPop({msg: \'当前第' + start + '页\'});">' + start + '</a>';
    h += '<a href="javascript:;" onclick="pageNext();">下一页</a>';
    h += '<a href="javascript:;" onclick="pageEnd();">尾页</a>';
    h += '</span>';
    $('#pageHtml').html(h);
}
//首页
function pageHome(){
    window.location.href = pages.url;
}
//上一页
function pagePre(){
    var start = pages.start * 1 - 1;
    if (start <= 0) {
        msgPop({msg: '已经是第一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//下一页
function pageNext(){
    var start = pages.start * 1 + 1;
    var pageTotal = Math.ceil(pages.total / pages.limit);
    if (start >= pageTotal) {
        msgPop({msg: '已经是最后一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//尾页
function pageEnd(){
    var pageTotal = Math.ceil(pages.total / pages.limit);
    window.location.href = pages.url + '?start=' + pageTotal;
}


//上传HTML
function uploadHtml(type){
    var selid = type==1 ? 'big' : 'more';
    var h = '';
    if (type == 2) {
        h += '<div id="moreDiv">';
    }
    h += '<div style="padding: 5px 0; width: 100px; background: #0088cc; text-align: center; font-size: 12px; color: #fff; border-radius: 5px; position: relative;">上传图片</div>';
    h += '<input type="file" id="' + selid + 'Upload" style="width: 150px; height: 30px; opacity: 0; cursor: pointer; position: relative; top: -25px; left: -20px;" onchange="uploadImage(' + type + ')"/>';
    if (type == 2) {
        h += '</div>';
    }
    h += '<div id="' + selid + 'Show" style="margin-top: -20px; width: 500px; height: 300px; background: #eee; overflow-x: hidden; overflow-y: auto;"></div>';
    $('#' + selid + 'Out').html(h);
}


////抓图
//function uploadImage() {
//    var img = $("#viewimg"), dataURL, fileObj = $('#' + selid + 'Upload')[0],
//        windowURL = window.URL || window.webkitURL, reader = new FileReader();
//    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
//    img.attr("src", dataURL); //显示图片
//    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
//    reader.onload = function (e){
//        //console.log(e.target.result);
//        //$('#img')[0].value = e.target.result; //这一步是关键
//        showImg(e.target.result);
//    }
//}


//抓图
function uploadImage() {
    var fileObj = $('#bigUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        imageUpload({img: res.target.result});
    }
}


//图片上传
function imageUpload(e){
    $.ajax({
        type: apis.imageUpload.method,
        url: apis.imageUpload.url,
        data: { img: e.img },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            showImg({domain: res.data.domain, img: res.data.img});
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//layer弹窗
function layerMsgErr(e){
    layer.msg(e,{icon: 0,time: 1000});
    return
}


//layer弹窗
function layerMsgOk(e){
    layer.msg(e,{icon: 1,time: 1000});
    return
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//显示前几个字符
function strPrefix(str, limit){
    if (str.length > limit) {
        return str.substring(0, limit) + '...';
    } else {
        return str;
    }
}


//显示后几个字符
function strSuffix(str, limit){
    //console.log(str.length, limit);
    if (str.length > limit) {
        return '...' + str.slice(- limit);
    } else {
        return str;
    }
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//退出登录
function logout(){
    console.log('退出登录');
    delCache({key: adminLogin});
    window.location.href = '/admin/login.html';
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}